from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU

class OrderSerializer(serializers.ModelSerializer):

    class Meta:
        model=OrderInfo
        fields="__all__"
#提供字段给 OrderGoodsSerializer
class OrderSKUserializer(serializers.ModelSerializer):
    class Meta:
        model=SKU
        fields=("name","default_image_url")
#提供字段给 OrderModelSerializer
class OrderGoodsSerializer(serializers.ModelSerializer):
    #sku里又有两个字段是SKU的,所以要重写sku,从SKU序列化器取,
    sku=OrderSKUserializer(read_only=True)
    class Meta:
        model=OrderGoods
        fields=("sku","price","count")

class OrderModelSerializer(serializers.ModelSerializer):
    #重写skus字段,这个字段的内容包含了OrderGoods的两个字段,所以要在OrderGoods的序列化器取
    #本模型与OrderGoods是一队多的关系,所以这里要写many
    skus=OrderGoodsSerializer(read_only=True,many=True)

    class Meta:
        model=OrderInfo
        fields="__all__"